package com.ibeeking.gmc.global.g.rest.service.impl;

import com.ibeeking.gmc.global.g.rest.service.ITenantUserService;
import com.ibeeking.found.common.response.ResponseCodeEnum;
import com.ibeeking.found.common.utils.DataUtils;
import com.ibeeking.gmc.global.api.common.dto.TenantUserDTO;
import com.ibeeking.gmc.global.api.feign.UpmsTenantUserClient;

import com.ibeeking.nematos.core.utils.SpringBeanUtil;

import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @ClassName TenantServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2020-12-24 09:21
 **/
@Service
public class TenantUserServiceImpl  implements ITenantUserService {

    @Resource
    private UpmsTenantUserClient upmsTenantUserClient;

    @Override
    public List<TenantUserDTO> queryEffectUserByTenantId(String tenantId) {
        if(StringUtils.isEmpty(tenantId)) throw new BusinessException("租户ID不能为空");
        Integer tId = Integer.valueOf(tenantId);
        ResponseResult<List<TenantUserDTO>> responseResult = upmsTenantUserClient.queryByTenantId(tId);
        DataUtils.trueThrowException(!ResponseCodeEnum.SUCCESS.getCode().equals(responseResult.getCode()), responseResult.getMsg());
        List<TenantUserDTO> tenantUserDTOS = responseResult.getData();
        if(Collections3.isEmpty(tenantUserDTOS)) return new ArrayList<>();
        return JsonUtils.jsonToList(tenantUserDTOS, TenantUserDTO.class);
    }

    private static ITenantUserService getBean() {
        return SpringBeanUtil.getBean(ITenantUserService.class);
    }
}
